<?php


namespace app\index\controller;


use app\index\model\ActionRole;
use app\index\model\Menu;
use app\index\model\MenuRole;
use think\Request;
use think\response\Json;

class MenuController extends BaseController
{
    protected $request;

    public function __construct(Request $request = null)
    {
        parent::__construct($request);
        $this->request=$request;
    }

    /**
     * getUserMenu
     * 重新获取用户菜单
     * @url menu/menu
     * @method POST
     * @request string token
     * @request int user_id
     * @return Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @author Asira
     * @date 2021/1/25
     */
    public function getUserMenu() :Json{
        $token=input('post.token');
        $id=input('post.user_id/d');
        $user_data=$this->user_session;
        if (!($user_data['id']===$id && $user_data['token']===$token)) {
            return json(['code' => 401, 'msg' => '请登录']);
        }

        $menus=MenuRole::getUserMenu($user_data);
        /*$menus=[];
        foreach ($menu as $key=>$value){
            $menus[]=$value['url_path'];
        }*/
        $action_role=ActionRole::getViewActionRole($user_data);
        return json(['code'=>0,'menu'=>$menus,'action_role'=>$action_role]);
    }

    public function getMenuByUser(){
        $id=input('post.id/d',0);
        $menu_id=MenuRole::getMenuByUser($id);
        return json(['code'=>0,'keys'=>$menu_id]);
    }

    public function getMenuByRole(){
        $role=input('post.id/d',0);
        $menu_id=MenuRole::getMenuByRole($role);
        return json(['code'=>0,'keys'=>$menu_id]);
    }

    public function getMenuByRoleDetail(){
        $role=input('post.id/d',0);
        $menu_id=MenuRole::getMenuByRoleDetail($role);
        return json(['code'=>0,'keys'=>$menu_id]);
    }

    public function getMenuByGroup(){
        $role=input('post.id/d',0);
        $menu_id=MenuRole::getMenuByGroup($role);
        return json(['code'=>0,'keys'=>$menu_id]);
    }

    public function getMenuParam(){
        $menu_list=Menu::getMenuList();
        return json(['code'=>0,'menu_list'=>$menu_list]);
    }

    public function createMenu(){
        $menu_name=input('post.name');
        $menu_id=input('post.menu_id',0);
        $path=input('post.path');
        $MenuClass=new Menu();
        $level=1;
        if($menu_id) {
            $menu_data=$MenuClass->where('id', '=', $menu_id)->where('del_flag','=',0)->find();
            if(empty($menu_data)){
                return json(['code'=>1,'msg'=>'未找到菜单']);
            }
            $level=2;
        }
        if(empty($path)){
            return json(['code'=>1,'msg'=>'权限名称不能为空']);
        }
        $menu_data=$MenuClass->where('url_path', '=', $path)->where('del_flag','=',0)->find();
        if(!empty($menu_data)){
            return json(['code'=>1,'msg'=>'权限名称已被使用']);
        }
        if(empty($menu_name)){
            return json(['code'=>1,'msg'=>'权限名称不能为空']);
        }
        $result=$MenuClass->data(['menu_name'=>$menu_name,'url_path'=>$path,'p_id'=>$menu_id,'menu_level'=>$level])->allowField(true)->save();
        if($result){
            return json(['code'=>0,'msg'=>'添加成功']);
        }
        return json(['code'=>1,'msg'=>'添加失败']);
    }
}